package com.pickgliss.ui.controls.tab
{
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.ui.ComponentFactory;
	import com.pickgliss.ui.controls.container.BoxContainer;
	import com.pickgliss.ui.controls.container.HBox;
	import com.pickgliss.ui.controls.container.VBox;
	import com.pickgliss.ui.image.Image;
	import com.pickgliss.utils.Directions;
	import com.pickgliss.utils.DisplayUtils;
	import com.pickgliss.utils.ObjectUtils;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	public class TabPanel extends Component
	{
		public static const P_backgound:String = "backgound";
		public static const P_containerX:String = "containerX";
		public static const P_containerY:String = "containerY";
		public static const P_paddingChange:String = "paddingChange";
		public static const P_tabBoxDirection:String = "tabBoxDirection";
		public static const P_tabrefresh:String = "tabrefresh";
		
		public function TabPanel()
		{
			super();
		}
		
		protected var _backStyle:String;
		protected var _backgound:Image;
		protected var _container:Sprite;
		protected var _containerX:Number;
		protected var _containerY:Number;
		protected var _padding:Number;
		protected var _tabBox:BoxContainer;
		protected var _tabBoxDirection:int;
		protected var _tabHBox:HBox;
		protected var _tabList:Vector.<DisplayObject>;
		protected var _tabVBox:VBox;
		
		public function addTab(tab:DisplayObject):void
		{
			if(tab == null) return;
			_tabList.push(tab);
			onPropertiesChanged(P_tabrefresh);
		}
		
		public function addTabs(tabs:Vector.<DisplayObject>):void
		{
			if(tabs == null || tabs.length == 0)clearTabs();
			_tabList = tabs;
			onPropertiesChanged(P_tabrefresh);
		}
		
		public function set backStyle(stylename:String):void
		{
			if(_backStyle == stylename || stylename == "") return;
			_backStyle =  stylename;
			backgound = ComponentFactory.Instance.creat(_backStyle);
		}
		
		public function set backgound(image:Image):void
		{
			if(_backgound == image) return;
			ObjectUtils.disposeObject(_backgound);
			_backgound = image;
			onPropertiesChanged(P_backgound);
		}
		
		public function clearTabs():void
		{
			_tabBox.disposeAllChildren();
		}
		
		public function set containerX(value:Number):void
		{
			if(_containerX == value) return;
			_containerX = value;
			onPropertiesChanged(P_containerX);
		}
		
		public function set containerY(value:Number):void
		{
			if(_containerY == value) return;
			_containerY = value;
			onPropertiesChanged(P_containerY);
		}
		
		override public function dispose():void
		{
			ObjectUtils.removeChildAllChildren(_container);
			ObjectUtils.disposeObject(_container);
			_container = null;
			
			ObjectUtils.disposeObject(_tabBox);
			_tabBox = null;
			
			ObjectUtils.disposeObject(_backgound);
			_backgound = null;
			
			_tabList = null;
		}
		
		public function getTabBox():BoxContainer
		{
			if(_tabBoxDirection == Directions.DIRECTION_T || _tabBoxDirection == Directions.DIRECTION_B)
			{
				if(_tabVBox == null) _tabVBox = new VBox();
				return _tabVBox;
			}else if(_tabBoxDirection == Directions.DIRECTION_L || _tabBoxDirection == Directions.DIRECTION_R)
			{
				if(_tabHBox == null) _tabHBox = new HBox();
				return _tabHBox;
			}
			return null;
		}
		
		public function set padding(value:Number):void
		{
			if(_padding == value) return;
			_padding = value;
			onPropertiesChanged(P_paddingChange);
		}
		
		public function setContent(child:DisplayObject):void
		{
			ObjectUtils.removeChildAllChildren(_container);
			if(child == null) return;
			_container.addChild(child);
		}
		
		public function set tabBoxDirection(direction:int):void
		{
			if(_tabBoxDirection == direction) return;
			_tabBoxDirection = direction;
			onPropertiesChanged(P_tabBoxDirection);
		}
		
		public function set tabPadding(value:Number):void
		{
			_tabBox.spacing = value;
		}
		
		override protected function addChildren():void
		{
			if(_backgound) addChild(_backgound);
			if(_tabBox) addChild(_tabBox);
			if(_container) addChild(_container);
		}
		
		protected function addTabToBox():void
		{
			if(_tabList.length <= 0)return;
			_tabBox.beginChanges();
			for(var i:int = 0;i<_tabList.length;i++)
			{
				_tabBox.addChild(_tabList[i]);
			}
			_tabBox.commitChanges();
		}
		
		override protected function init():void
		{
			_tabBox = getTabBox();
			_container = new Sprite();
			_padding = 0;
			_tabList = new Vector.<DisplayObject>();
			super.init();
		}
		
		override protected function onProppertiesUpdate():void
		{
			super.onProppertiesUpdate();
			if(_changedPropeties[P_tabrefresh] || _changedPropeties[P_tabBoxDirection] || _changedPropeties[P_paddingChange])
			{
				if(_changedPropeties[P_tabrefresh])addTabToBox();
				if(_tabBox!= getTabBox())
				{
					_tabBox = getTabBox();
					addTabToBox();
				}
				updateBoxAndContainerPos();
			}
			
			if(_changedPropeties[P_height] || _changedPropeties[P_width])
			{
				if(_backgound)
				{
					_backgound.width = _width;
					_backgound.height = _height;
				}
			}
			
			if(_changedPropeties[P_containerX] || _changedPropeties[P_containerY])
			{
				_container.x = _containerX;
				_container.y = _containerY;
			}
		}
		
		protected function updateBoxAndContainerPos():void
		{
			if(_tabBoxDirection == Directions.DIRECTION_T)
			{
				_tabBox.y = 0;
				_container.y = _tabBox.height + _padding;
			}
			else if(_tabBoxDirection == Directions.DIRECTION_B)
			{
				_tabBox.y = _height - _tabBox.height;
				_container.y = 0;
			}else if(_tabBoxDirection == Directions.DIRECTION_L)
			{
				_tabBox.x = 0;
				_container.x = _tabBox.width + _padding;
			}else if(_tabBoxDirection == Directions.DIRECTION_R)
			{
				_tabBox.x = _width - _tabBox.width;
				_container.x = 0;
			}
		}
	}
}